home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.1 / Examples2 / AmigaGuide / AG_V39 / Src / nodehost.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  9.1 KB  |  355 lines

  1. /* nodehost.c
  2.  *
  3.  * (c) Copyright 1992-1999 Amiga, Inc.  All rights reserved.
  4.  *
  5.  * This software is provided as-is and is subject to change; no warranties
  6.  * are made.  All use is at your own risk.  No liability or responsibility
  7.  * is assumed.
  8.  *
  9.  * Written by David N. Junod
  10.  *
  11.  * Example of a Dynamic Node Host.  This example is useful for determining
  12.  * what nodes an AmigaGuide database is calling when it brings up the
  13.  * "Can't locate node" requester.
  14.  */
  15.  
  16. #include <exec/types.h>
  17. #include <intuition/intuition.h>
  18. #include <intuition/screens.h>
  19. #include <utility/hooks.h>
  20. #include <utility/tagitem.h>
  21. #include <libraries/amigaguide.h>
  22. #include <dos/dos.h>
  23. #include <dos/dosextens.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27.  
  28. #include <clib/exec_protos.h>
  29. #include <clib/dos_protos.h>
  30. #include <clib/intuition_protos.h>
  31. #include <clib/graphics_protos.h>
  32. #include <clib/utility_protos.h>
  33. #include <clib/amigaguide_protos.h>
  34.  
  35. #include <pragmas/exec_pragmas.h>
  36. #include <pragmas/dos_pragmas.h>
  37. #include <pragmas/intuition_pragmas.h>
  38. #include <pragmas/graphics_pragmas.h>
  39. #include <pragmas/utility_pragmas.h>
  40. #include <pragmas/amigaguide_pragmas.h>
  41.  
  42. /*****************************************************************************/
  43.  
  44. #define    DB(x)    x
  45.  
  46. #define    ASM    __asm __saveds
  47. #define    REG(x)    register __ ## x
  48.  
  49. /*****************************************************************************/
  50.  
  51. extern void kprintf (void *,...);
  52. ULONG ASM dispatchAmigaGuideHost (REG (a0) struct Hook * h, REG (a2) STRPTR db, REG (a1) Msg msg);
  53.  
  54. /*****************************************************************************/
  55.  
  56. extern struct Library *SysBase, *DOSBase;
  57. struct Library *IntuitionBase, *GfxBase, *UtilityBase, *AmigaGuideBase;
  58.  
  59. /*****************************************************************************/
  60.  
  61. main (int argc, char **argv)
  62. {
  63.     struct Hook hook;
  64.     APTR hh;
  65.  
  66.     if (IntuitionBase = OpenLibrary ("intuition.library", 39))
  67.     {
  68.     GfxBase = OpenLibrary ("graphics.library", 39);
  69.     UtilityBase = OpenLibrary ("utility.library", 39);
  70.  
  71.     if (AmigaGuideBase = OpenLibrary ("amigaguide.library", 39))
  72.     {
  73.         /* Initialize the hook */
  74.         hook.h_Entry = (HOOKFUNC)dispatchAmigaGuideHost;
  75.  
  76.         /* Add the AmigaGuideHost to the system */
  77.         if (hh = AddAmigaGuideHostA (&hook, "ExampleHost", NULL))
  78.         {
  79.         printf ("Added AmigaGuideHost 0x%lx\n", hh);
  80.  
  81.         /* Wait until we're told to quit */
  82.         Wait (SIGBREAKF_CTRL_C);
  83.  
  84.         printf ("Remove AmigaGuideHost 0x%lx", hh);
  85.  
  86.         /* Try removing the host */
  87.         while (RemoveAmigaGuideHostA (hh, NULL) > 0)
  88.         {
  89.             /* Wait a while */
  90.             printf (".");
  91.             Delay (250);
  92.         }
  93.         printf ("\n");
  94.         }
  95.         else
  96.         {
  97.         printf ("Couldn't add AmigaGuideHost\n");
  98.         }
  99.  
  100.         /* close the library */
  101.         CloseLibrary (AmigaGuideBase);
  102.     }
  103.     else
  104.     {
  105.         printf ("couldn't open amigaguide.library V39\n");
  106.     }
  107.  
  108.     CloseLibrary (UtilityBase);
  109.     CloseLibrary (GfxBase);
  110.     CloseLibrary (IntuitionBase);
  111.     }
  112.     else
  113.     {
  114.     printf ("requires 3.0\n");
  115.     }
  116. }
  117.  
  118. /*****************************************************************************/
  119.  
  120. #define    TEMP_NODE "This AmigaGuideHost is an example, that can also\nbe used as a debugging tool.\n"
  121. #define    LINK "Link: "
  122.  
  123. /*****************************************************************************/
  124.  
  125. struct TextAttr TOPAZ8  = {"topaz.font", 8, NULL, NULL};
  126. struct TextAttr TOPAZ8B = {"topaz.font", 8, FSF_BOLD, NULL};
  127.  
  128. /*****************************************************************************/
  129.  
  130. /* We really need the screen, rectangle, and pen spec. */
  131. VOID Display (struct opNodeIO * onm)
  132. {
  133.     struct TagItem *attrs = onm->onm_Attrs;
  134.     struct IntuiText it1 = {NULL};
  135.     struct IntuiText it2 = {NULL};
  136.     struct Rectangle *rect = NULL;
  137.     struct NewWindow nw = {NULL};
  138.     struct Screen *scr = NULL;
  139.     struct IntuiMessage *msg;
  140.     struct Window *win;
  141.     UWORD *pens = NULL;
  142.     BOOL going = TRUE;
  143.     LONG width = 0L;
  144.     WORD w = 640;
  145.     WORD h = 200;
  146.     WORD dif;
  147.  
  148.     /* Get attributes, could be NULL */
  149.     if (attrs)
  150.     {
  151.     scr = (struct Screen *) GetTagData (HTNA_Screen, NULL, attrs);
  152.     pens = (UWORD *) GetTagData (HTNA_Pens, NULL, attrs);
  153.     rect = (struct Rectangle *) GetTagData (HTNA_Rectangle, NULL, attrs);
  154.     }
  155.  
  156.     /* Prepare the IntuiText */
  157.     it1.FrontPen = it2.FrontPen = ((pens) ? pens[SHADOWPEN] : 1);
  158.     it1.DrawMode = it2.DrawMode = JAM1;
  159.     it1.ITextFont = &TOPAZ8;
  160.     it2.ITextFont = &TOPAZ8B;
  161.     it1.IText = LINK;
  162.     it2.IText = onm->onm_Node;
  163.  
  164.     /* Get the width of the first string */
  165.     width = IntuiTextLength (&it1);
  166.     it2.LeftEdge = (SHORT) width;
  167.  
  168.     /* Add in the length of the node name */
  169.     width += IntuiTextLength (&it2);
  170.  
  171.     /* Link the text */
  172.     it1.NextText = &it2;
  173.  
  174.     /* Prepare the window */
  175.     nw.IDCMPFlags = IDCMP_VANILLAKEY | IDCMP_MOUSEBUTTONS;
  176.     nw.Flags = WFLG_BORDERLESS | WFLG_SMART_REFRESH | WFLG_NOCAREREFRESH \
  177.       |WFLG_ACTIVATE;
  178.     nw.Width = 8 + width + 8;
  179.     nw.Height = 16;
  180.     nw.Screen = scr;
  181.     nw.Type = (scr) ? CUSTOMSCREEN : WBENCHSCREEN;
  182.  
  183.     /* Cache the screen size */
  184.     if (scr)
  185.     {
  186.     w = scr->Width;
  187.     h = scr->Height;
  188.     }
  189.  
  190.     /* See if we have a open help window */
  191.     if (rect)
  192.     {
  193.     /* Center the window within the help window */
  194.     nw.LeftEdge = rect->MinX + ((rect->MaxX - nw.Width) / 2);
  195.     nw.TopEdge = rect->MinY + ((rect->MaxY - nw.Height) / 2);
  196.     }
  197.     /* No help window, so go off the screen */
  198.     else if (scr)
  199.     {
  200.  
  201.     /*
  202.      * Center the window horizontally under the mouse and place it
  203.      * vertically over the mouse position.
  204.      */
  205.     nw.LeftEdge = scr->MouseX - (nw.Width / 2);
  206.     nw.TopEdge = scr->MouseY - (nw.Height - 2);
  207.  
  208.     /* Make sure the window can open */
  209.     nw.LeftEdge = (nw.LeftEdge < 0) ? 0 : nw.LeftEdge;
  210.     nw.TopEdge = (nw.TopEdge < 0) ? 0 : nw.TopEdge;
  211.     }
  212.  
  213.     /* Make sure window is on-screen */
  214.     dif = (nw.LeftEdge + nw.Width) - w;
  215.     nw.LeftEdge = (dif > 0) ? nw.LeftEdge - dif : nw.LeftEdge;
  216.     dif = (nw.TopEdge + nw.Height) - h;
  217.     nw.TopEdge = (dif > 0) ? nw.TopEdge - dif : nw.TopEdge;
  218.  
  219.     /* Open the temporary window */
  220.     if (win = OpenWindow (&nw))
  221.     {
  222.     /* Clear the window background */
  223.     SetAPen (win->RPort, ((pens) ? pens[SHADOWPEN] : 1));
  224.     RectFill (win->RPort, 0, 0, (win->Width - 1), (win->Height - 1));
  225.     SetAPen (win->RPort, ((pens) ? pens[SHINEPEN] : 2));
  226.     RectFill (win->RPort, 1, 1, (win->Width - 2), (win->Height - 2));
  227.  
  228.     /* Print the text */
  229.     PrintIText (win->RPort, &it1, 8, 4);
  230.  
  231.     /* Keep on going til the going gets tough */
  232.     while (going)
  233.     {
  234.         /* Wait around for something eventful */
  235.         Wait (1L << win->UserPort->mp_SigBit);
  236.  
  237.         /* Pull each message and handle it */
  238.         while (msg = (struct IntuiMessage *) GetMsg (win->UserPort))
  239.         {
  240.         switch (msg->Class)
  241.         {
  242.             case IDCMP_MOUSEBUTTONS:
  243.             /* Stop if we were touched */
  244.             if (msg->Code == SELECTDOWN)
  245.             {
  246.                 going = FALSE;
  247.             }
  248.             break;
  249.  
  250.             case IDCMP_VANILLAKEY:
  251.             /* Stop on significant keypress */
  252.             if ((msg->Code == 27) || (msg->Code == 13))
  253.             {
  254.                 going = FALSE;
  255.             }
  256.             break;
  257.         }
  258.  
  259.         ReplyMsg ((struct Message *) msg);
  260.         }
  261.     }
  262.  
  263.     /* Close the window */
  264.     CloseWindow (win);
  265.     }
  266. }
  267.  
  268. /*****************************************************************************/
  269.  
  270. /* This is your AmigaGuideHost dispatch hook.  It will never run on your own process. */
  271. ULONG ASM dispatchAmigaGuideHost (REG (a0) struct Hook * h, REG (a2) STRPTR db, REG (a1) Msg msg)
  272. {
  273.     struct opNodeIO *onm = (struct opNodeIO *) msg;
  274.     struct opFindHost *ofh;
  275.     ULONG retval = 0;
  276.  
  277.     switch (msg->MethodID)
  278.     {
  279.         /* Does this node belong to you? */
  280.     case HM_FINDNODE:
  281.         ofh = (struct opFindHost *) msg;
  282.         DB (kprintf ("Find [%s] in %s\n", ofh->ofh_Node, db));
  283.  
  284.         /* See if they want to find our table of contents */
  285.         if ((Stricmp (ofh->ofh_Node, "main")) == 0)
  286.         {
  287.         /*
  288.          * Return TRUE to indicate that it's your node, otherwise
  289.          * return FALSE.
  290.          */
  291.         DB (kprintf ("found main\n"));
  292.         retval = TRUE;
  293.         }
  294.         else
  295.         {
  296.         /* Display the name of the node */
  297.         Display (onm);
  298.  
  299.         /*
  300.          * Return TRUE to indicate that it's your node, otherwise
  301.          * return FALSE.
  302.          */
  303.         retval = FALSE;
  304.         DB (kprintf ("didn't find\n"));
  305.         }
  306.         break;
  307.  
  308.         /* Open a node. */
  309.     case HM_OPENNODE:
  310.         DB (kprintf ("Open [%s] in %s\n", onm->onm_Node, db));
  311.  
  312.         /* See if they want to display our table of contents */
  313.         if ((Stricmp (onm->onm_Node, "main")) == 0)
  314.         {
  315.         /* Provide the contents of the node */
  316.         onm->onm_DocBuffer = TEMP_NODE;
  317.         onm->onm_BuffLen = strlen (TEMP_NODE);
  318.         }
  319.         else
  320.         {
  321.         /* Display the name of the node */
  322.         Display (onm);
  323.  
  324.         /*
  325.          * Indicate that we want the node removed from our database,
  326.          * and that we handled the display of the node
  327.          */
  328.         onm->onm_Flags |= (HTNF_CLEAN | HTNF_DONE);
  329.         }
  330.  
  331.         /* Indicate that we were able to open the node */
  332.         retval = TRUE;
  333.         break;
  334.  
  335.         /* Close a node, that has no users. */
  336.     case HM_CLOSENODE:
  337.         DB (kprintf ("Close [%s] in %s\n", onm->onm_Node, db));
  338.  
  339.         /* Indicate that we were able to close the node */
  340.         retval = TRUE;
  341.         break;
  342.  
  343.         /* Free any extra memory */
  344.     case HM_EXPUNGE:
  345.         DB (kprintf ("Expunge [%s]\n", db));
  346.         break;
  347.  
  348.     default:
  349.         DB (kprintf ("Unknown method %ld\n", msg->MethodID));
  350.         break;
  351.     }
  352.  
  353.     return (retval);
  354. }
  355.